﻿/*
   Author: Tomer Paz
   BOGO.com
   2008
*/

/* the DB client system */

/* the DBClient system is based on AMF3 and relies on web server to use AMFPHP (>=2.0!!) */

package fui.FUIData {
	import flash.display.Sprite;
	import flash.net.ObjectEncoding;
	import fui.FUIData.PHPRemoting.*;
	import fui.FUIData.IDBClient;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	
	public class DBClient extends EventDispatcher implements IDBClient
	{
		// singleton
		private static var _instance:DBClient;
		//AMF gateway
		private var _gateway:String = "http://bogo.com/BOGO/amfphp/gateway.php";
		private static const _Procedure:String = "FUIServices.";
		// PHPRemote object
		private var _p:Remoting;
		// RPC map
		private var _RPC_map:Object;
		// respone arrays
		
		private var _ResponseArray:Array;
		
		private var _UserData:Array;
		private var _UpdateRC:Array;
		private var _GameData:Array;
		private var _PlayersData:Array;		//holds All players data
		private var _BoardData:Array;
		private var _PropertiesList:Array;

		
		public function DBClient(parameter:SingletonEnforcer):void
			{
			}
			
		/* real constructor. Args: HostName = Web server host name for AMFPHP */
		public static function getInstance(HostName:String, RootSprite:Sprite):DBClient
			{
	        if(_instance == null) 
				{
	            _instance = new DBClient(new SingletonEnforcer());
		
				// AMF3 is our desired protocol version BUT AMFPHP does not support it before v2.0 :(
				//	_instance.objectEncoding = ObjectEncoding.AMF0;

				_instance.init_RPC_map();
				
				// Gateway.php url for NetConnection
				_instance._gateway = "http://" + HostName + "/amfphp/gateway.php";
				
				// get PHPRemoting instance to work with
				_instance._p = Remoting.getInstance(RootSprite.root,_instance._gateway);
				}
			return _instance;
			}
		
		// mapper of RPC Response functions
		
		private function init_RPC_map():void
			{
			_instance._RPC_map = new Object( );
			_instance._RPC_map[ "verify_user_by_name"	] = _instance.verify_user_by_name_response;
			_instance._RPC_map[ "add_new_player_wrapper"] = _instance.register_new_user_response;
			_instance._RPC_map[ "get_game_data"		    ] = _instance.get_game_data_response;
			_instance._RPC_map[ "get_player_data"		] = _instance.get_player_data_response;
			_instance._RPC_map[ "get_properties_list"	] = _instance.get_properties_list_response;
			}
		
		public function get CallResponse():Object
		{
			return new Object().rc = false;//
		}
		
		public function get user_data():Array
		{
			return _instance._UserData;
		}
		public function get register_new_user_result():Boolean
		{
			trace(_instance._ResponseArray);
			return true;
		}
		public function get game_data():Array
			{
			return _instance._GameData;
			}
		
		public function player_data(uid:int):Array
			{
			for (var i = 0; i < _instance._PlayersData.length; i++)
				{
				if (_instance._PlayersData[i].UID == uid)
					break;
				}
			return _instance._PlayersData[i];
			}
		public function get players_data():Array
			{
			// just return the fucking players data array
			return _instance._PlayersData;
			}
			
		public function get board_data_by_id():Array
			{
			return _instance._BoardData;
			}
			
		public function get properties_list():Array
			{
			return _instance._PropertiesList;
			}
			
		/* private functions */	
		private function verify_user_by_name_response(re:PHPResult):void
		{
			_instance._ResponseArray = re.result[0] as Array;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		private function register_new_user_response(re:PHPResult):void
		{
			trace("register_new_user_response");
			_instance._ResponseArray = re.result[0] as Array;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		private function get_game_data_response(re:PHPResult):void
		{
			//trace (re.result);
			_instance._GameData = re.result[0] as Array;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		private function get_player_data_response(re:PHPResult):void
			{
			//trace (re.result);
			
			if (null == _instance._PlayersData)
				_instance._PlayersData = new Array();
			_instance._PlayersData.push(re.result[0] as Array);
			dispatchEvent(new Event(Event.CHANGE));
			}
        private function get_board_data_by_id_response(re:PHPResult):void
			{
			//trace (re.result);
			_instance._BoardData = re.result[0] as Array;
			dispatchEvent(new Event(Event.COMPLETE));
			}
        private function get_properties_list_response(re:PHPResult):void
			{
			trace (re.result);
			_instance._PropertiesList = re.result as Array;	// array of associative arrays
			dispatchEvent(new Event(Event.COMPLETE));
			}
		

		
		/* a Generic function */
		
		public function CallStoredProcedure(StoreProcedure:String, ... args):void
		{
			trace("---------------CallStoredProcedure-----------------");
			trace(args);
			
			var Procedure:String = _Procedure + StoreProcedure;
			var MySQLCall:PHPCall= new PHPCall(Remoting.PHP, this._RPC_map[StoreProcedure], Procedure, args);
		}
	}
}
class SingletonEnforcer {}